<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/4
 * Time: 23:55
 */

namespace Home\Controller;

class OrderController extends HomeController
{

    public function index()
    {
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $books = M('Order_type')->select();
        $server = D('Server')->limit(1)->select();
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link',$link);
        $this->assign('advs',$advs);
        $this->assign('server', $server);
        $this->assign('books', $books);
        $this->display();
    }

    public function show()
    {
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $id = I('id', '');
        $roots = M('Order_root')->where(array('bid' => $id, 'level' => 1))->select();
        $order = M('Order_type')->where('id=' . $id)->find();
        foreach ($roots as $i => $root) {
            $tmp = $this->getChildren($root['id']);
            if (sizeof($tmp) > 0) {
                $roots[$i]['child'] = $tmp;
                foreach ($roots[$i]['child'] as $j => $ts) {
                    $tp = $this->getChildren($ts['id']);
                    if (sizeof($tp) > 0) {
                        $roots[$i]['child'][$j]['child'] = $tp;
                    }
                }
            }
        }

        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link',$link);
        $this->assign('advs',$advs);
        $this->assign('bid', $id);
        $this->assign('order', $order);
        $this->assign('roots', $roots);
        $server = M('Server')->limit(1)->select();
        $this->assign('server', $server);
        $this->display();
    }

    public function getChildren($pid)
    {
        $children = M('Order_root')->where('pid=' . $pid)->select();
        return $children;
    }

    public function add()
    {

        $data['bid'] = I('bid', '');
        $data['more'] = I('more', '');
        $data['username'] = I('username', '');
        $data['phone'] = I('phone', '');
        $data['create_time'] = date('Y-m-d H:i:s');
        $answers = I('answers', '');
        $dataList = array();
        $res = M('order')->data($data)->add();
        if ($res) {
            foreach ($answers as $count => $answer) {
                $arr = explode(":", $answer);
                array_push($dataList,array('oid' => $res, 'pid' => $arr[0], 'aid' => $arr[1], 'value' => $arr[2]));
            }
            $rset = M('Order_detail')->addAll($dataList);
            if ($rset){
                $this->ajaxReturn(array('status'=>1,'info'=>'下单成功'),'JSON');
            }else{
                $this->ajaxReturn(array('status'=>0,'info'=>'下单失败-1!'),'JSON');
            }
        }else{
            $this->ajaxReturn(array('status'=>0,'info'=>'下单失败-2!'),'JSON');
        }
    }


    public function getLocate()
    {
        $demand = M('Main_info')->where(array('type' => '需求方', 'finish' => 0, 'show'=>1))->select();
        $supply = M('Main_info')->where(array('type' => '供应方', 'finish' => 0, 'show'=>1))->select();
        $finish = M('Main_info')->where(array('finish' => 1, 'show'=>1))->select();
        $this->ajaxReturn(array('demand' => $demand, 'supply' => $supply, 'finish' => $finish), "JSON");
    }

    public function getReqLocate()
    {
        $demand = M('Main_info')->where(array('type' => '需求方', 'finish' => 0, 'show'=>1))->select();
        $this->ajaxReturn(array('demand' => $demand), "JSON");
    }

    public function getSupLocate()
    {
        $supply = M('Main_info')->where(array('type' => '供应方', 'finish' => 0, 'show'=>1))->select();
        $this->ajaxReturn(array('supply' => $supply), "JSON");
    }

    public function getFinLocate()
    {
        $finish = M('Main_info')->where(array('finish' => 1, 'show'=>1))->select();
        $this->ajaxReturn(array('finish' => $finish), "JSON");
    }

}